"""
代码实现过程
    1 发送请求
    2 获取数据
    3 解析数据
    4 格式化输出效果

"""

import requests
import prettytable as pt    # 格式化输出
import json
import yuding

f = open('city.json', encoding='utf-8')
txt = f.read()
json_data = json.loads(txt)      # 转成字典类型数据

# print(json_data)
from_station = input('请输入你出发的城市：')
to_station = input('请输入你到底的城市：')
date = input('请输入你要出发的日期(格式：2001-01-01)：')
# print(json_data[from_station])
# print(json_data[to_station])




url = f'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={date}&leftTicketDTO.from_station={json_data[from_station]}&leftTicketDTO.to_station={json_data[to_station]}&purpose_codes=ADULT'
headers = {
    'Cookie': '_uab_collina=166636335346373008938754; JSESSIONID=0616799455376C6A67DE09E87C9682B3; BIGipServerotn=1123025418.64545.0000; BIGipServerpool_passport=82051594.50215.0000; guidesStatus=off; highContrastMode=defaltMode; cursorStatus=off; RAIL_EXPIRATION=1666649865367; RAIL_DEVICEID=sufFct_c1GBKTtAskG3DXTHdw3WAgC0P48nfEy1o9LJ2TGTerV9mVaB-XUz2FIoVqNaZik3TLHvpf4IXtGz_sWsHrwgUdyCKea2Sz6w7OkfZC0eFAjrM83HxbnGjRFEEkqsQSAZ_kTUmqUGPL5M1sfWTbg5DMXvR; fo=zvms2rhgiyplzx5c4O1T-M_08Dkb0EMF1iWEOoyxtBQgQ9BGhXI1h8mXAwGnfDVfr6V5vrmi39k8omhavbnrMJvykVQkPfT9fNw1K6hUI-9l827wTBb8omjFzEhHsNw4lH4BHWdm3StQZsZHyjgtBbfoOVoSl8fJEH5GUIgU-kUcb4AufTV_mqQwhjs; route=9036359bb8a8a461c164a04f8f50b252; _jc_save_fromStation=%u6210%20%20%u90FD%u4E1C%2CWEI; _jc_save_toStation=%u6CF8%u5DDE%2CLUE; _jc_save_toDate=2022-10-21; _jc_save_wfdc_flag=dc; _jc_save_fromDate=2022-10-25',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36 Edg/106.0.1370.47'
}


resp = requests.get(url=url, headers=headers)
resp.encoding = 'utf-8'
# print(resp.json())
# 解析数据 提取想要的内容数据

tb = pt.PrettyTable()
tb.field_names = [          # 字段名，第一行
        '序号',
        '车次号',
        '出发时间',
        '到底时间',
        '耗时',
        '特等座',
        '一等座',
        '二等座',
]
page = 1

result = resp.json()['data']['result']
for index in result:
    # print(index.split('|'))     # 以 | 分割成列表
    info = index.split('|')
    num = info[3]       # 车次号
    start_time = info[8]       # 出发时间
    end_time = info[8]      # 到达时间
    use_time = info[10]     # 耗时
    business_block = info[32]   # 特等座
    first_class_seat = info[31]     # 一等座
    secomd_class_seat = info[30]    # 二等座

    dit = {
        '车次号': info[3],
        '出发时间': info[8],
        '到底时间': info[9],
        '耗时': info[10],
        '特等座': info[32],
        '一等座': info[31],
        '二等座': info[30]
    }

    # page = 0        # 确定想要的数据位置
    # for i in index.split('|'):
    #     print(i, page, sep='|')     # sep 打印以|分割
    #     page += 1
    tb.add_row([    # 内容
        page,
        num,
        start_time,
        end_time ,
        use_time,
        business_block,
        first_class_seat,
        secomd_class_seat
    ])
    page += 1
print(tb)
word = input('请输入你想要购买的车票(输入序号)：')
# wei = input('请输入你想要的座位号(A,B,C,D,F)：')
yuding.get_train(int(word), from_station, to_station , date)


